package com.ruoyi.order.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.order.domain.DTO.VerificationResult;
import com.ruoyi.order.domain.DTO.VerifyEntity;
import com.ruoyi.order.domain.VO.VerificationDetail;
import com.ruoyi.order.domain.VerificationRecord;
import com.ruoyi.order.service.IVerificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/verification")
public class VerificationController {

    @Autowired
    private IVerificationService verificationService;

    /**
     * 生成核销码
     */
    @PostMapping("/generate")
    public AjaxResult generateVerificationCode(@RequestParam Long orderId, @RequestParam Long merchantId,@RequestParam Long userId) {
        try {
            VerificationRecord record = verificationService.generateVerificationRecord(orderId, merchantId,userId);
            return AjaxResult.success("核销码生成成功", record.getVerifyCode());
        } catch (Exception e) {
            return AjaxResult.error("生成核销码失败: " + e.getMessage());
        }
    }



    /**
     * 执行核销
     */
    @PostMapping("/verify")
    public AjaxResult verifyOrder(@RequestBody VerifyEntity verifyEntity) {

        VerificationResult result = verificationService.verifyOrder(verifyEntity.getVerifyCode(), verifyEntity.getMerchantId());
        if (result.isSuccess()) {
            return AjaxResult.success(result.getMessage(), result);
        } else {
            return AjaxResult.error(result.getMessage());
        }
    }


    /**
     * 查询核销记录
     */
    @GetMapping("/selectList")
    public  AjaxResult   selectList(){
        List<VerificationDetail>   result=verificationService.selectList();
        return   AjaxResult.success("查询所有核销信息成功",result);
    }

}